<?php
  include '../JHeader.php';
  require '../Database/Database.php';

  if(isset($_POST['edit']))
  {
      $id       = $_POST['id'];
      $name     = $_POST['league_name'];
      $shortcut = $_POST['league_shortcut'];

      $sql  = 'UPDATE Leagues SET ';

      $values  = array();
      $columns = array();

      echo '<h2>&Aumlnderungen</h2><table>';

      if($name != '')
      {
          $values[]  = $name;
          $columns[] = 'Name';

          echo '<tr><td>' . $_POST['old_name'] . ' -> ' . $name . '<br /></td></tr>';
      }
      if($shortcut != '')
      {
          $values[]  = $shortcut;
          $columns[] = 'Shortcut';

          echo '<tr><td>' . $_POST['old_shortcut'] . ' -> ' . $shortcut . '<br /></td></tr>';
      }

      for($i = 0; $i < sizeof($values); $i++)
      {
          $sql .= $columns[$i] . ' = \'' . $values[$i] . '\'';
          if(($i + 1) < sizeof($values))
          {
              $sql .= ', ';
          }
      }

      $sql .= ' WHERE ID_League = ' . $id;

      $db = new Database();
      $db->Connect();
      $db->ExecSQL($sql);
      $db->Disconnect();

      echo '</table><br /><b>Ligadetails wurden ge&aumlndert!</b><br />';
  }
  else if(isset($_GET['id']))
  {
      $id = $_GET['id'];
      $db = new Database();
      $db->Connect();
      $leagues = $db->Select('Leagues', 'ID_League = \'' . $id . '\'');
      $db->Disconnect();
      if($leagues)
      {
          $league = $leagues[0];
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <script type="text/javascript" language="JavaScript" src="../js_helpfuncs/helpfuncs.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Bearbeite "<?php echo $league->Name ?>"</title>
    </head>
    <body>
        <h1><?php echo $league->Name ?> bearbeiten</h1>
        <form name="edit_form" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
            <div class="changeable_values">
                <table>
                    <tr>
                        <td>Name: <input type="text" name="league_name" size="25" /></td>
                        <td>aktuell: <?php echo $league->Name ?></td>
                    </tr>
                    <tr>
                       <td>Kürzel: <input type="text" name="league_shortcut" size="10" /></td>
                       <td>aktuell: <?php echo $league->Shortcut ?></td>
                    </tr>
                </table>
            </div>
            <div class="constant_values">
                <table>
                    <tr>
                        <td>Simulation: <?php echo $league->Game ?></td>
                    </tr>
                </table>
            </div>
            <div>
                <input type="hidden" name="id" value="<?php echo $id ?>" />
                <input type="hidden" name="old_name" value="<?php echo $league->Name ?>" />
                <input type="hidden" name="old_shortcut" value="<?php echo $league->Shortcut ?>" />
                <input type="submit" name="edit" value="Ändern" onclick="return checkAndConfirm( 'edit_form', 'Wirklich ändern?')" />
            </div>
        </form>
    </body>
</html>
<?php
    }
    else
    {
       echo 'League with id ' . $_GET['id'] . ' not found.';
    }
  }
  else
  {
    echo 'No league id.';
  }
?>